package com.qf.storage;

import com.qf.service.Book;
import com.qf.service.api.IBookController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: Song
 * @Date: 2022-03-17 23:32
 * dwadawdawdaw
 */
@Controller
//@RestController
//@RequestMapping("/book")
public class BookController implements IBookController {

    @GetMapping("/books")
    @ResponseBody
    public List<Book> getBooksByIds(String ids) {
        //调用方法传来的id
        List<Integer> list = Arrays.stream(ids.split(",")).map(id -> Integer.parseInt(id)).collect(Collectors.toList());
        List<Book> books = new ArrayList<>();
        for (Integer id : list) {
            Book book = new Book();
            book.setId(id);
            books.add(book);
        }
        System.out.println("ids = " + ids);
        return books;
    }

    @Override
    public Book getBookById(Integer id) {
//        int i = 1 / 0;
        Book book = new Book();
        book.setId(id);
        System.out.println("getbookById:" + id);
        return book;
    }

    @Override
    public void deleteBookById(Integer id) {
        System.out.println("id = " + id);
    }

    @Override
    public Book addBook(Book book) {
        return book;
    }

    @PostMapping("/add")
    public String addBook2(@RequestBody Book book) {
        System.out.println("addBook2 = " + book);
        return "redirect:/index";
    }

    @Override
    public void updateBook(Book book) {
        System.out.println("book = " + book);
    }
}
